<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
    class admin_bannerController extends JController 
    {
        function __construct( $default = array())	
        {
            parent::__construct( $default );
	}
        
        function display($tpl = null) 
        {   
		$view = JRequest::getCmd('view');
		if(empty($view)) {
                    JRequest::setVar('view', 'dashboard');
		};
		parent::display($tpl);
	}
        function disable()
        {
            $get = JRequest::get('get',4);
            $db = &JFactory::getDbo();
            $sql = "UPDATE #__hc_banner
                    SET disabled = 1
                    WHERE id = '".$get['id']."' ";
            $db->setQuery($sql);
            $db->query();
            $this->setRedirect('index.php?option=com_admin_banner');
        }
        function enable()
        {
            $get = JRequest::get('get',4);
            $db = &JFactory::getDbo();
            $sql = "UPDATE #__hc_banner
                    SET disabled = 0
                    WHERE id = '".$get['id']."' ";
            $db->setQuery($sql);
            $db->query();
            $this->setRedirect('index.php?option=com_admin_banner');
        }
        function del_banner()
        {
            $post = JRequest::get('post',4);
            $db = &JFactory::getDbo();
            $sql = "UPDATE #__hc_banner
                    SET deleted = 1
                    WHERE id = '".$post['id']."' ";
            $db->setQuery($sql);
            $db->query();
            $this->setRedirect('index.php?option=com_admin_banner','Xóa Thành Công.');
        }
        
        function add_banner()
        {
            $post = JRequest::get('post',4);
           // print_r($post);die;
            $allowedExts = array("jpg", "jpeg", "gif", "png");
            $extension = end(explode(".", $_FILES["file"]["name"]));
            
            if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/png")
            || ($_FILES["file"]["type"] == "image/pjpeg"))
            && in_array($extension, $allowedExts))
              {
              
                if (file_exists("components/com_admin_banner/assets/images/" . $_FILES["file"]["name"]))
                  {
                  echo $_FILES["file"]["name"] . " Đã Tồn Tại. ";
                  }
                else
                  {
                  move_uploaded_file($_FILES["file"]["tmp_name"],
                  "components/com_admin_banner/assets/images/" . $_FILES["file"]["name"]);
                  
                  }
                }
              
            else
              {
              $this->setRedirect('index.php?option=com_admin_banner', 'File Không Đúng Định Dạng.');
              return;
              }
                $row =  & JTable::getInstance('hc_banner', 'Table');
             	$row->image = $_FILES["file"]["name"];	 
                $row->disabled = 0;				 
                $row->deleted = 0;
                	 
                if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                                              JError::raiseError(500, $row->getError() );
                                                 }  
                $this->setRedirect('index.php?option=com_admin_banner', 'Thêm Banner Mới Thành Công.');
        }
        
    }
?>